﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using System.Data.Common;
using System.Data;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.IO;
using AMStudio.Log;

namespace AMStudio.Common
{
    public class ProductDB
    {
        private string directory = @"Config\Production";
        private string suffix = @".recipe";
        private List<string> productNames = new List<string>();
        public List<string> ProductNames
        {
            get { return productNames; }
            set { productNames = value; }
        }
        private List<ProductSettings> products = new List<ProductSettings>();
        public List<ProductSettings> Products
        {
            get { return products; }
            set { products = value; }
        }

        public ProductDB()
        {
            InitProducts();
        }

        public void SaveOrUpdateProductSettings(ProductSettings item)
        {
            if (ProductNames.Exists((s) =>
            {
                return s == item.Name;
            }))
            {
                Update(item);
            }
            else
            {
                Add(item);
                productNames.Add(item.Name);
                products.Add(item);
            }
        }

        private bool Add(ProductSettings item)
        {
            SaveProduct(directory + @"\" + item.Name + suffix, item);
            return true;
        }

        private bool Update(ProductSettings item)
        {
            SaveProduct(directory + @"\" + item.Name + suffix, item);
            return true;
        }

        public bool Delete(string name)
        {
            File.Delete(directory + @"\" + name + suffix);
            productNames.Remove(name);
            ProductSettings pro = products.Find((p) => { return p.Name == name; });
            products.Remove(pro);
            return true;
        }

        public ProductSettings GetProducts(string name)
        {
            foreach (ProductSettings p in products)
            {
                if (p.Name == name)
                {
                    return p;
                }
            }
            return null; 
        }

        /// <summary>
        /// 查询所有信息
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, ProductSettings> SelectAllMapper()
        {
            var list = new Dictionary<string, ProductSettings>();
            string directory = @"Config\Production";
            if (Directory.Exists(directory))
            {
                string[] files = Directory.GetFiles(directory, "*.recipe");
                foreach (string f in files)
                {
                    try
                    {
                        list.Add(f.Replace(directory + @"\", string.Empty),
                            GetProduct(f));
                    }
                    catch (Exception ex)
                    {
                        //TODO
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// 查询所有信息
        /// </summary>
        /// <returns></returns>
        public void InitProducts()
        {
            products = new List<ProductSettings>();
            productNames.Clear();

            if (Directory.Exists(directory))
            {
                string[] files = Directory.GetFiles(directory, "*.recipe");
                foreach (string f in files)
                {
                    try
                    {
                        productNames.Add(f.Replace(directory + @"\", string.Empty).Replace(suffix, string.Empty));
                        products.Add(GetProduct(f));
                    }
                    catch (Exception ex)
                    {
                        //TODO
                    }
                }
            }
        }
        private ProductSettings GetProduct(string myFileName)
        {
            ProductSettings myConfig = null;
            if (File.Exists(myFileName))
            {
                using (Stream s = new FileStream(myFileName, FileMode.Open))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(ProductSettings));
                    myConfig = xs.Deserialize(s) as ProductSettings;

                    myConfig.Name = Path.GetFileNameWithoutExtension(myFileName);
                }
                if (myConfig == null)
                {
                    myConfig = new ProductSettings();
                    SaveProduct(myFileName, myConfig);
                }
            }
            else
            {
                myConfig = new ProductSettings();
                XmlSerializer xs = new XmlSerializer(typeof(ProductSettings));
                using (Stream s = new FileStream(myFileName, FileMode.Create))
                {
                    xs.Serialize(s, myConfig);

                    
                }
            }
            return myConfig;
        }
        private void SaveProduct(string myFileName, ProductSettings myConfig)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ProductSettings));
            using (Stream s = new FileStream(myFileName, FileMode.Create))
            {
                xs.Serialize(s, myConfig);
            };
        }
    }
}
